import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException, NoAlertPresentException
from common.Utils import BlogDriver


class BlogDetail:
    driver = ""
    url = ""

    def __init__(self):
        self.driver = BlogDriver.driver
        self.url = "http://8.137.19.140:9090/blog_detail.html?blogId=38068"
        self.driver.get(self.url)

    def handle_empty_alert(self):
        """处理空文本警告弹窗的通用方法"""
        try:
            # 等待弹窗出现（最多5秒）
            alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
            alert_text = alert.text.strip()  # 去除空字符
            if not alert_text:  # 判断弹窗文本是否为空
                print("检测到空文本警告弹窗，已关闭")
                alert.accept()  # 关闭弹窗
                BlogDriver.GetScreeShot()  # 截图记录
            return True
        except NoAlertPresentException:
            print("未检测到弹窗")
            return False

    def BlogLoginDetailTest(self):
        try:
            # 先处理可能存在的空弹窗
            self.handle_empty_alert()

            # 原有元素检查逻辑
            self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
            self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
            self.driver.find_element(By.CSS_SELECTOR, "#detail")
            self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.right > div > div.operating > button:nth-child(1)")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.right > div > div.operating > button:nth-child(2)")
            BlogDriver.GetScreeShot()
            time.sleep(2)

        except UnexpectedAlertPresentException:
            # 处理操作中突然出现的空弹窗
            self.handle_empty_alert()
            raise  # 抛出异常，提示测试中断

    def NotBlogLoginDetailTest(self):
        try:
            # 先处理可能存在的空弹窗
            self.handle_empty_alert()

            # 原有元素检查逻辑
            self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
            self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
            self.driver.find_element(By.CSS_SELECTOR, "#detail")
            self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.right > div > div.operating > button:nth-child(1)")
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.container > div.right > div > div.operating > button:nth-child(2)")
            BlogDriver.GetScreeShot()
            time.sleep(2)

        except UnexpectedAlertPresentException:
            # 处理操作中突然出现的空弹窗
            self.handle_empty_alert()
            assert False, "未登录状态下出现空文本警告弹窗，测试异常"
